--- 模块功能：lcddemo
-- @module lcd
-- @author Dozingfiretruck
-- @release 2021.01.25

-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "gpio测试"
VERSION = "1.0.0"

log.info("项目名称：", PROJECT, VERSION)

-- sys库是标配
_G.sys = require("sys")

-- UI带屏的项目一般不需要低功耗了吧, Air101/Air103设置到最高性能
if mcu and (rtos.bsp() == "AIR101" or rtos.bsp() == "AIR103" or rtos.bsp() == "AIR601" ) then
    mcu.setClk(240)
end

--[[
-- LCD接法示例
LCD管脚       Air780E管脚    Air101/Air103管脚   Air105管脚
GND          GND            GND                 GND
VCC          3.3V           3.3V                3.3V
SCL          (GPIO11)       (PB02/SPI0_SCK)     (PC15/HSPI_SCK)
SDA          (GPIO09)       (PB05/SPI0_MOSI)    (PC13/HSPI_MOSI)
RES          (GPIO01)       (PB03/GPIO19)       (PC12/HSPI_MISO)
DC           (GPIO10)       (PB01/GPIO17)       (PE08)
CS           (GPIO08)       (PB04/GPIO20)       (PC14/HSPI_CS)
BL(可以不接)  (GPIO22)       (PB00/GPIO16)       (PE09)


提示:
1. 只使用SPI的时钟线(SCK)和数据输出线(MOSI), 其他均为GPIO脚
2. 数据输入(MISO)和片选(CS), 虽然是SPI, 但已复用为GPIO, 并非固定,是可以自由修改成其他脚
3. 若使用多个SPI设备, 那么RES/CS请选用非SPI功能脚
4. BL可以不接的, 若使用Air10x屏幕扩展板,对准排针插上即可
]]

--添加硬狗防止程序卡死
if wdt then
    wdt.init(9000)--初始化watchdog设置为9s
    sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
end

local rtos_bsp = rtos.bsp()

-- 根据不同的BSP返回不同的值
-- spi_id,pin_reset,pin_dc,pin_cs,bl
function lcd_pin()
    if rtos_bsp == "AIR101" then
        return 0,pin.PB03,pin.PB01,pin.PB04,pin.PB00
    elseif rtos_bsp == "AIR103" then
        return 0,pin.PB03,pin.PB01,pin.PB04,pin.PB00
    elseif rtos_bsp == "AIR105" then
        return 5,pin.PC12,pin.PE08,pin.PC14,pin.PE09
    elseif rtos_bsp == "ESP32C3" then
        return 2,10,6,7,11
    elseif rtos_bsp == "ESP32S3" then
        return 2,16,15,14,13
    elseif rtos_bsp == "EC618" then
        return 0,1,10,8,22
    elseif rtos_bsp == "EC718P" then
        return lcd.HWID_0,36,0xff,0xff,0xff -- 注意:EC718P有硬件lcd驱动接口, 无需使用spi,当然spi驱动也支持
    else
        log.info("main", "bsp not support")
        return
    end
end

local spi_id,pin_reset,pin_dc,pin_cs,bl = lcd_pin()

if spi_id ~= lcd.HWID_0 then
    spi_lcd = spi.deviceSetup(spi_id,pin_cs,0,0,8,20*1000*1000,spi.MSB,1,0)
    port = "device"
else
    port = spi_id
end

    --[[ 此为合宙售卖的1.8寸TFT LCD LCD 分辨率:128X160 屏幕ic:st7735 购买地址:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-24045920841.19.6c2275a1Pa8F9o&id=560176729178]]
    -- lcd.init("st7735",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 128,h = 160,xoffset = 0,yoffset = 0},spi_lcd)

    --[[ 此为合宙售卖的1.54寸TFT LCD LCD 分辨率:240X240 屏幕ic:st7789 购买地址:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-24045920841.20.391445d5Ql4uJl&id=659456700222]]
    --lcd.init("st7789",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 240,h = 320,xoffset = 0,yoffset = 0},spi_lcd)

    --[[ 此为合宙售卖的0.96寸TFT LCD LCD 分辨率:160X80 屏幕ic:st7735s 购买地址:https://item.taobao.com/item.htm?id=661054472686]]
    lcd.init("st7735v",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 1,w = 160,h = 80,xoffset = 0,yoffset = 24},spi_lcd)
    --如果显示颜色相反，请解开下面一行的注释，关闭反色
    --lcd.invoff()
    --如果显示依旧不正常，可以尝试老版本的板子的驱动
    -- lcd.init("st7735s",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 2,w = 160,h = 80,xoffset = 0,yoffset = 0},spi_lcd)

-- 不在内置驱动的, 看demo/lcd_custom
--lcd.setColor(0xFFFF,0x0000)
lcd.setColor(0x0000,0xFFFF)
--lcd.drawQrcode(40, 0, '山东邹平台子', 80)

--按键引脚定义
local key1 = pin.PA01 -- 左
local key2 = pin.PA07 -- 上
local key3 = pin.PA04 -- 中
local key4 = pin.PA00 -- 右
local key5 = pin.PB11 -- 下

-- 若固件支持防抖, 启用防抖
if gpio.debounce then
    gpio.debounce(key1, 20,1)
end

if gpio.debounce then
    gpio.debounce(key2, 20,1)
end

if gpio.debounce then
    gpio.debounce(key3, 20,1)
end

if gpio.debounce then
    gpio.debounce(key4, 20,1)
end

if gpio.debounce then
    gpio.debounce(key5, 20,1)
end

zhong=0
gpio.setup(key1,function (val)
	zuo=1
--	print('左')
	if val==0 then
		lvgl.indev_point_emulator_update(120,60,1)
	else
		lvgl.indev_point_emulator_update(120,60,0)
	end
--    lvgl.indev_point_emulator_update(20,40,1)
	end,gpio.PULLUP)

gpio.setup(key4,function (val)
	you=1
	--	print('右')
	if val==0 then
		lvgl.indev_point_emulator_update(10,20,1)
	else
		lvgl.indev_point_emulator_update(10,20,0)
	end
--    lvgl.indev_point_emulator_update(20,40,1)
	end,gpio.PULLUP)

gpio.setup(key2,function (val)
	shang=1
--	print('上')
 --   lvgl.indev_point_emulator_update(140,40,0)
	end,gpio.PULLUP)

gpio.setup(key5,function (val)
	xia=1
--	print('下')
--    lvgl.indev_point_emulator_update(140,40,1)
	end,gpio.PULLUP)

gpio.setup(key3,function (val)
	zhong=zhong+1
    if zhong==2 then
        zhong=0
    end
    print(zhong)
--	print('中')-- body
	end,gpio.PULLUP,gpio.FALLING

)



 --lcd.fill(0,20,100,40,0xf00f)
--lcd.clear()

--lvgl.init(160,80)--lvgl初始化

local function event_handler1(obj, event)
    if(event == lvgl.EVENT_PRESSED) then
	--	lvgl.indev_point_emulator_update(20,40,1)
		numb=numb-1
		abc=tostring(numb)
		lvgl.label_set_text(label3, 'jj');
        print("1_按了",abc)
    end
end

local function event_handler2(obj, event)
    if(event == lvgl.EVENT_PRESSED) then
	--	lvgl.indev_point_emulator_update(140,40,1)
		numb=numb+1
		abc=tostring(numb)
		lvgl.label_set_text(label3, 'fh')
        print("2_按了",abc)
    end
end
abc=1
function my_event_cb(obj, event)
    if event == lvgl.EVENT_PRESSED then
		abc=abc+45
        print("Pressed\n");
    elseif event == lvgl.EVENT_SHORT_CLICKED then
        print("Short clicked\n")
    elseif event == lvgl.EVENT_CLICKED then
        print("Clicked\n")
    elseif event == lvgl.EVENT_LONG_PRESSED then
        print("Long press\n")
    elseif event == lvgl.EVENT_RELEASED then
        print("Released\n")
    end
end
function my_event_cb2(obj, event)
    if event == lvgl.EVENT_PRESSED then
		abc=abc+10
        print("Pressed\n");
    elseif event == lvgl.EVENT_SHORT_CLICKED then
        print("Short clicked\n")
    elseif event == lvgl.EVENT_CLICKED then
        print("Clicked\n")
    elseif event == lvgl.EVENT_LONG_PRESSED then
        print("Long press\n")
    elseif event == lvgl.EVENT_RELEASED then
        print("Released\n")
    end
end
rtc.set({year=2021,mon=8,day=3,hour=17,min=8,sec=43})

log.info("lvgl", lvgl.init())

local list_demo = {}

local function event_handler(obj, event)
    if(event == lvgl.EVENT_CLICKED) then
        print(string.format("Clicked: %s\n", lvgl.list_get_btn_text(obj)));
    end
end

function list_demo.demo()
    --Create a list
    local list1 = lvgl.list_create(lvgl.scr_act(), nil);
    lvgl.obj_set_size(list1, 100, 50);
    lvgl.obj_align(list1, nil, lvgl.ALIGN_CENTER, 0, 0);

    --Add buttons to the list
    local list_btn;

    list_btn = lvgl.list_add_btn(list1, lvgl.SYMBOL_FILE, "New");
    lvgl.obj_set_event_cb(list_btn, event_handler);

    list_btn = lvgl.list_add_btn(list1, lvgl.SYMBOL_DIRECTORY, "Open");
    lvgl.obj_set_event_cb(list_btn, event_handler);

    list_btn = lvgl.list_add_btn(list1, lvgl.SYMBOL_CLOSE, "Delete");
    lvgl.obj_set_event_cb(list_btn, event_handler);

    list_btn = lvgl.list_add_btn(list1, lvgl.SYMBOL_EDIT, "Edit");
    lvgl.obj_set_event_cb(list_btn, event_handler);

    list_btn = lvgl.list_add_btn(list1, lvgl.SYMBOL_SAVE, "Save");
    lvgl.obj_set_event_cb(list_btn, event_handler);

    list_btn = lvgl.list_add_btn(list1, lvgl.SYMBOL_BELL, "Notify");
    lvgl.obj_set_event_cb(list_btn, event_handler);

    list_btn = lvgl.list_add_btn(list1, lvgl.SYMBOL_BATTERY_FULL, "Battery");
    lvgl.obj_set_event_cb(list_btn, event_handler);

return list_demo
end

gpio.setup(pin.PB06, nil,gpio.PULLUP)
gpio.caplevel(pin.PB06,0,function(val) print(val) end)




sys.taskInit(function()
	local cnt=0
	--创建容器
	local cont = lvgl.cont_create(nil, nil);

  --创建2个标签
   local label  = lvgl.label_create(cont, nil)
   local label1 = lvgl.label_create(cont, nil)
   local label2 = lvgl.label_create(cont, nil)
   lvgl.obj_set_pos(label, 5, 0)
   lvgl.obj_set_pos(label1, 95, 0)
   lvgl.label_set_text(label2, '更待何时')--设置标签内容
   lvgl.obj_set_pos(label2, 0, 26)
 --  lvgl.obj_set_pos(cont, 10, 30)

   local btn1= lvgl.btn_create(cont,nil)  --创建按钮
   lvgl.obj_set_pos(btn1, 2, 55)
   lvgl.obj_set_size(btn1, 50, 25)
   lvgl.obj_set_event_cb(btn1,my_event_cb)
   local label3 = lvgl.label_create(btn1, nil)
   lvgl.label_set_text(label3, '按钮1')--设置标签内容

   lvgl.indev_drv_register("pointer", "emulator")  --注册指针设备

   local cont2 = lvgl.cont_create(nil, nil);
   local label5  = lvgl.label_create(cont2, nil)
   lvgl.label_set_text(label5, '更待何时')--设置标签内容

 while 1 do
		local t = rtc.get()
		local dispStr =string.format("%04d",tostring(t.year))..'-'..string.format("%02d",tostring(t.mon))..'-'..string.format("%02d",tostring(t.day))
	 --   local dispStr ='时间: '..string.format("%3.2f",cnt)--浮点数转换成字符串
	--	local dispStr1=string.format("%3.2f",cnt)--浮点数转换成字符串
		local dispStr1 =string.format("%02d",tostring(t.hour))..':'..string.format("%02d",tostring(t.min))..':'..string.format("%02d",tostring(t.sec))

		lvgl.label_set_text(label, dispStr)  --设置标签内容
		lvgl.label_set_text(label1, dispStr1)--设置标签内容
		lvgl.label_set_text(label2, abc)--设置标签内容
		if zhong==0 then
            lvgl.scr_load(cont)--加载容器 显示标签
        elseif  zhong==1 then
            lvgl.scr_load(cont2)--加载容器 显示标签
        end
	    sys.wait(100)
    --    lvgl.scr_load(cont2)--加载容器 显示标签
    --    sys.wait(100)
    end
end)

sys.taskInit(function()
	
	while 1 do
	
		log.info('aa')
		sys.wait(1000)
	
	end

end)

-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!
